package cn.ad.server.web.spring.service.actions.handler;

import cn.ad.server.web.spring.model.sts.STSAdResponse;
import cn.ad.server.domain.ad.*;
import cn.ad.server.domain.enumration.ElementType;
import cn.ad.server.domain.enumration.MaterialFormat;
import org.springframework.util.StringUtils;

import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 * @author dongbin.yu
 * @from 2016-06-25
 * @since V1.0
 */
public class OriginAdFormatHandler extends AbstractAdFormatHandler {

    @Override
    public void matchCreative(AdSpace adSpace) {

        if (adSpace.getMatchStrategy() != null) {

//            List<Creative> creativeList = adSpace.getMatchStrategy().getCreatives();
            List<Creative> creativeList = adSpace.getMatchStrategy().getMatchCreatives();

            for(Iterator<Creative> iterator = creativeList.iterator(); iterator.hasNext();) {
                Creative c = iterator.next();

                if (adSpace.getAdTemplate().getId() != c.getAdTemplate().getId()) {
                    iterator.remove();
                }
            }

            if (creativeList.size() != 0) {
                adSpace.setMatchCreative(creativeList.get(new Random().nextInt(creativeList.size())));
            }

        }

    }

    @Override
    public void compositeMaterial(AdSpace localAdSpace, STSAdResponse.AdSpace adSpace, int os) {

        //原生读取广告位上面的模版
        List<AdMaterialGroup> materialGroups = localAdSpace.getMatchCreative().getAdMaterialGroups();
        localAdSpace.setMatchAdMaterialGroups(materialGroups);

        STSAdResponse.AdNative adNative = null;

        STSAdResponse.Creative creative = new STSAdResponse.Creative();
        STSAdResponse.AdNative[] adNatives = new STSAdResponse.AdNative[materialGroups.size()];

        
        Creative matchCreative = localAdSpace.getMatchCreative();
        int interactionType = matchCreative.getInteractionType();
        
        for (int j = 0; j < materialGroups.size(); j++) {
            AdMaterialGroup adMaterialGroup = materialGroups.get(j);
            adNative = new STSAdResponse.AdNative();
            adNative.setTemplateId(adMaterialGroup.getAdTemplate().getId());

            AdTemplateSpec adTemplateSpec = adMaterialGroup.getAdTemplateSpec();
            AdMaterial originMaterial = adMaterialGroup.getAdMaterial();

            adNative.setIndex(adTemplateSpec.getIndex());
            adNative.setIndexValue(adTemplateSpec.getIndexValue());
            adNative.setRequiredField(adTemplateSpec.getElementType());
            if (adTemplateSpec.getElementType() == ElementType.IMAGE.getValue()) {
                adNative.setRequiredValue(originMaterial.getMaterialUrl());
            } else if (adTemplateSpec.getElementType() == ElementType.TEXT.getValue()) {
                adNative.setRequiredValue(originMaterial.getMaterialText());
            }

//			//素材组上交互不使用,只使用创意级别的。
//            adNative.setActionType(interactionType);
//            if (adMaterialGroup.getInteractionType() == ActionType.LINK_JUMP.getValue()) {
//                if (adMaterialGroup.getInteractionRedirects() == null) {
//                    logger.error("material id ({}) link_jump is not define", originMaterial.getId());
//                } else {
//                    adNative.setActionValue(adMaterialGroup.getInteractionRedirects().getTargetUrl());
//                }
//            } else if (adMaterialGroup.getInteractionType() == ActionType.APP_DOWNLOAD.getValue()) {
//
//                if (adMaterialGroup.getInteractionApp() == null) {
//                    logger.error("material id ({}) app_download is not define", originMaterial.getId());
//                } else {
//                    adNative.setActionValue(adMaterialGroup.getInteractionApp().getDownloadUrl());
//                }
//            }
//            //交互添加。
//            if (interactionType == ActionType.LINK_JUMP.getValue()) {
//                InteractionRedirects interactionRedirects = matchCreative.getInteractionRedirects();
//                if(interactionRedirects==null){
//                	logger.error("creative id ({}) link_jump is not define", matchCreative.getId());
//                }else{                	
//                	adNative.setActionValue(interactionRedirects.getTargetUrl());
//                }
//            } else if (interactionType == ActionType.APP_DOWNLOAD.getValue()) {
//                InteractionApp interactionApp = matchCreative.getInteractionApp();
//                if(interactionApp==null){
//                	logger.error("creative id ({}) app_download is not define", matchCreative.getId());
//                }else{                	
//                	adNative.setActionValue(interactionApp.getDownloadUrl());
//                }
//            }
            
            if (originMaterial.getMaterialFormat() > 0) {
                adNative.setType(MaterialFormat.materialFormatMap.get(originMaterial.getMaterialFormat()).getName());
            }

            adNatives[j] = adNative;

        }

        creative.setAdNative(adNatives);
        creative.setCid(localAdSpace.getMatchCreative().getId());

        // 填充event
        //Creative matchCreative = localAdSpace.getMatchCreative();
        interactionHandler(matchCreative,creative, os);

        //填充印象对象
        String impressionTracking = matchCreative.getImpressionTracking();
        if (StringUtils.hasText(impressionTracking)) {
            creative.setImpression(impressionTracking.split("\\|\\|\\|"));
        }

        String clicksTracking = matchCreative.getClicksTracking();
        if (StringUtils.hasText(impressionTracking)) {
            creative.setClick(clicksTracking.split("\\|\\|\\|"));
        }

        // 设置广告位创意
        adSpace.setCreative(new STSAdResponse.Creative[] { creative });

    }
}
